package com.baibei.ebec.home.index.holder;

import android.content.Context;
import android.support.design.widget.RaeTabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.baibei.basic.SimpleViewHolder;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.model.Area;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 船的地区分类
 * Created by ChenRui on 2017/8/4 0004 16:23.
 */
public class JewelryTypeViewHolder extends SimpleViewHolder implements RaeTabLayout.OnTabSelectedListener {
    /**
     * 回调
     */
    public interface OnAreaListener {

        /**
         * 地区点击的回调
         *
         * @param area area
         */
        void onAreaClickListener(Area area);
    }

    private OnAreaListener mListener;

    @BindView(R2.id.tablayout)
    RaeTabLayout mTabLayout;

    private boolean isFirst = true;

    /**
     * @param isTabMode 是否显示TAB模式
     */
    public JewelryTypeViewHolder(View itemView, OnAreaListener listener, boolean isTabMode) {
        super(itemView);
        mListener = listener;
        ButterKnife.bind(this, itemView);
        Context context = itemView.getContext();
        mTabLayout.addOnTabSelectedListener(this);
        if (!isTabMode) {
            mTabLayout.setSelectedTabIndicatorHeight(0);
        }
        mTabLayout.addTab(newTab(context, context.getString(R.string.gold_jewelry), com.baibei.module.R.mipmap.icon_gold, true));
        mTabLayout.addTab(newTab(context, context.getString(R.string.silver_jewelry), com.baibei.module.R.mipmap.icon_silver, false));
        mTabLayout.addTab(newTab(context, context.getString(R.string.platinum_jewelry), com.baibei.module.R.mipmap.icon_platinum, false));
    }

    private RaeTabLayout.Tab newTab(Context context, String title, int icon, boolean selectable) {
        RaeTabLayout.Tab tab = mTabLayout.newTab();
        TextView view = (TextView) LayoutInflater.from(context).inflate(R.layout.item_area, null, false);
        view.setText(title);
        view.setCompoundDrawablesWithIntrinsicBounds(0, icon, 0, 0);
        view.setSelected(selectable);
        tab.setCustomView(view);
        return tab;
    }

    private void onTabClick(RaeTabLayout.Tab tab) {
        int position = tab.getPosition();
        if (position == 0 && isFirst) {
            isFirst = false;
            return;
        }

        Area area;
        if (position == 0) {
            area = Area.XAU;
        } else if (position == 1) {
            area = Area.XAG;
        } else {
            area = Area.XPT;
        }

        mListener.onAreaClickListener(area);
    }


    @Override
    public void onTabSelected(RaeTabLayout.Tab tab) {
        onTabClick(tab);
    }

    @Override
    public void onTabUnselected(RaeTabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(RaeTabLayout.Tab tab) {
        onTabClick(tab);
    }
}
